//
//  LSLiveViewController.m
//  LookScore
//
//  Created by wr on 2018/3/20.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLiveViewController.h"
#import "CustomSegmentedView.h"
#import "LSLiveCell.h"
#import "LSLiveSectionView.h"
#import "LSBottomView.h"
#import "LSSocketManeger.h"
#import "LSLiveModel.h"
#import "LSMarchDetailModel.h"
#import "LSLiveSectionFooterView.h"
@interface LSLiveViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic, strong) NSArray * titleArr;
@property (nonatomic, assign) NSInteger segmentIndex;

//@property (nonatomic, )

@end

@implementation LSLiveViewController{
    
    LSBottomView * _bottomView ;
   
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, 300) style:UITableViewStylePlain];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = @[].mutableCopy;
    }
    
    return _dataArr;
}

- (void)setEventListArray:(NSMutableArray *)eventListArray{
     _eventListArray = eventListArray;
    if (self.segmentIndex == 0){
       
        NSIndexSet * indexSet = [[NSIndexSet alloc] initWithIndex:1];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
    }
    
}
- (void)setReportListArray:(NSMutableArray *)reportListArray{
    _reportListArray = reportListArray;
    if (self.segmentIndex == 0) {
        
        NSIndexSet * indexSet = [[NSIndexSet alloc] initWithIndex:0];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
    }
    
    //    [self.tableView reloadData];
}
- (void)setLiveDataEventArray:(NSMutableArray *)liveDataEventArray{
    _liveDataEventArray = liveDataEventArray;
    if (self.segmentIndex == 1){
        
        [self.tableView reloadData];
    }
   
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleArr = @[@"事件统计",@"文字直播"];
//    [self.view addSubview: [self tableHeadView]];
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    [self.view addSubview:self.tableView];
     self.tableView.sd_layout
    .leftEqualToView(self.view)
    .topEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomEqualToView(self.view).offset(-LSNavBarHeight);


     self.tableView.tableHeaderView = [self tableHeadView];
    //添加底部的view；
    [self addBottomView];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    }
    
}





- (UIView *)tableHeadView{
    
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, 44)];
    backView.backgroundColor = LSBackGroundColor;
    CustomSegmentedView *aCustomSegmentedView=[[CustomSegmentedView alloc]initWidthCustomSegmentedViewFrame:CGRectMake(LSScareValue(220, LSScreenWidth)/2, 10, LSScreenWidth-LSScareValue(220, LSScreenWidth), 24) titlesArray:self.titleArr seletedTitleColor:[UIColor whiteColor] normalTitleColor:LSSubColorFF9E44 seletedBgColor:LSSubColorFF9E44 normalBgColor:[UIColor whiteColor] bordeColor:LSSubColorFF9E44  borderWidth:1 seletedCustomViewIndex:^(NSInteger index) {
        self.segmentIndex = index;
        [self isHiddenBottenWithIndex:index];
        [self.tableView reloadData];
        
    }];
    [aCustomSegmentedView selectedDefaultSegmentedIndex:0];
    [backView addSubview: aCustomSegmentedView];
    return backView;
}

- (void)addBottomView{
    NSArray * dataArray = @[@"入球",@"点球",@"角球",@"换人",@"换入",@"换出",@"红牌",@"黄牌",@"助攻",@"标注",@"乌龙球",@"任意球"];
    NSArray * imgArray = @[@"jin",@"dian",@"jiao",@"huan",@"huanru",@"huanchu",@"hong",@"huang",@"zhu",@"biao",@"wulong",@"renyi"];
    _bottomView = [[LSBottomView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 60) withArray:dataArray withImgArray:imgArray];

    [self.view addSubview:_bottomView];
    _bottomView.sd_layout
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .topSpaceToView(self.tableView, 0)
    .heightIs(62);
    
}

#pragma mark -- UITableViewDelegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (self.segmentIndex == 1) {
            return 0;
        }
        return self.reportListArray.count;
    }else {
        if (self.segmentIndex == 0) {
            
            return self.eventListArray.count;
        }
        return self.liveDataEventArray.count;
    }

   
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section < 1) {
        LSLiveOneCell * cell = [LSLiveOneCell liveOneTableViewCellBytableView:tableView];
//        UIView * backBgView = [[UIView alloc] init];
//        cell.backgroundView = backBgView;
//        if (indexPath.row == 1) {
//            backBgView.backgroundColor = LSBackGroundColor;
//        }
        cell.backgroundColor = LSBackGroundColor;
        cell.dataArray = self.reportListArray[indexPath.row];
        return cell;
    }else{
        
        
        if (self.segmentIndex == 0) {
            LSLiveCell * cell = [LSLiveCell liveTableViewCellByTabView:tableView];
            cell.eventModel = self.eventListArray[indexPath.row];
            if (indexPath.row == 0) {
                cell.topView.hidden = YES;
            }else if (indexPath.row == 9){
                
                cell.bottomView.hidden = YES;
            }
            return cell;
        }else{
            LSLiveDataCell * cell = [LSLiveDataCell liveDataTableViewCellByTabView:tableView];
            cell.guestId = self.model.GuestTeamId;
            cell.hostId = self.model.HomeTeamId;
            cell.liveDataModel = self.liveDataEventArray[indexPath.row];
            if (indexPath.row == 0) {
                cell.topView.hidden = YES;
            }else if (indexPath.row == 9){
                
                cell.bottomView.hidden = YES;
            }
            return cell;
        }
    }
   
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        //我传一个数组到cell里面 只能算出cell上某一个view的高度
        AutoCellHeightDataSettingBlock dataSetting = ^(UITableViewCell *cell) {
            LSLiveOneCell *myCell = (LSLiveOneCell *)cell;
            myCell.dataArray = self.reportListArray[indexPath.row];
        };
        NSArray * array = self.reportListArray[indexPath.row];
        NSInteger i ;
        if (array.count % 5 != 0) {
            i = array.count / 5 + 1;
        }else{
            i = array.count / 5;
            
        }
        MyLog(@"%f",[tableView cellHeightForIndexPath:indexPath cellClass:[LSLiveOneCell class] cellContentViewWidth:[tableView cellContentViewWith] cellDataSetting:dataSetting]*i);
      return  [tableView cellHeightForIndexPath:indexPath cellClass:[LSLiveOneCell class] cellContentViewWidth:[tableView cellContentViewWith] cellDataSetting:dataSetting]*i;
        
    }
    
    return 45;
}
//- (void)setModel:(LSMarchDetailModel *)model{
//    _model = model;
//    
//    [self.tableView reloadData];
//    
//}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section ==1) {
        if (self.model.MatchRealDateTime == nil || [self.model.MatchRealDateTime isEqualToString:@""]) {
            
            return nil;
        }else{
            
            UIView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
            headerView.backgroundColor = LSBackGroundColor;
            if (!headerView) {
                headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 30)];
                
                LSLiveSectionFooterView * footerView = [[LSLiveSectionFooterView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 30)];
                footerView.backgroundColor = LSBackGroundColor;
                footerView.model = self.model;
                [headerView addSubview:footerView];
                
            }
            return headerView;
        }
        
    }
    
    return nil;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else{
        if (self.model.MatchRealDateTime == nil || [self.model.MatchRealDateTime isEqualToString:@""]) {
            return 0.01;
        }
        return 30;
    }
}


#pragma mark -- 隐藏底部的view

- (void)isHiddenBottenWithIndex:(NSInteger )index{
    
    if (index == 0) {
        _bottomView.hidden = NO;
        self.tableView.sd_resetLayout.leftEqualToView(self.view).topEqualToView(self.view).rightEqualToView(self.view).bottomEqualToView(self.view).offset(-60.0);
    }else{
        _bottomView.hidden = YES;
        self.tableView.sd_resetLayout.leftEqualToView(self.view).topEqualToView(self.view).rightEqualToView(self.view).bottomEqualToView(self.view).offset(0.0);
    }
    
}



- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
